package com.allwees.bs.c.module.user.mapper;

import com.google.common.collect.Lists;
import com.allwees.bs.c.module.user.dto.StoreCollectionDto;
import com.allwees.bs.c.module.user.entity.StoreCollectionEntity;
import com.allwees.bs.core.modelbase.page.PageParams;
import com.allwees.bs.core.modelbase.page.PaginateDTO;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CollectionMapper {

    public PaginateDTO<StoreCollectionDto> entity2StoreCollDto(Page<StoreCollectionEntity> page, PageParams pageParams) {
        List<StoreCollectionEntity> entities = page.getContent();
        long total = page.getTotalElements();
        List<StoreCollectionDto> list = Lists.newArrayList();
        entities.forEach(entity -> {
            StoreCollectionDto storeCollectionDto = new StoreCollectionDto(entity);
            //收藏列表中的店铺必定是收藏了的
            storeCollectionDto.getStoreDto().setUserIsCollection(true);
            list.add(storeCollectionDto);
        });
        PaginateDTO<StoreCollectionDto> pageResult = PaginateDTO.of(total, entities.size(), pageParams);
        pageResult.setResults(list);
        return pageResult;
    }

}
